package me.tuyou.mybatis.test.interfaces.mapper;

import me.tuyou.mybatis.test.interfaces.po.Teacher;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TeacherMapper {

    @Delete("drop table if exists teacher")
    void dropTable();

    @Insert("create table teacher (name varchar(32) not null, subject varchar(32) default null, primary key (name))")
    void createTable();

    @Insert("insert into teacher values ('张三', '数学'), ('李四', '语文'), ('王五', '物理')")
    int initData();

    @Select("select * from teacher")
    @Results({
            @Result(property = "subject", column = "subject"),
            @Result(property = "teach", column = "subject", one = @One(
                    select = "me.tuyou.mybatis.test.interfaces.mapper.SubjectMapper.get",
                    fetchType = FetchType.LAZY)
            )
    })
    List<Teacher> selectAll();

    @Select("select * from teacher where name = #{0}")
    @Results({
            @Result(property = "subject", column = "subject"),
            @Result(property = "teach", column = "subject", one = @One(
                    select = "me.tuyou.mybatis.test.interfaces.mapper.SubjectMapper.get",
                    fetchType = FetchType.LAZY)
            )
    })
    Teacher selectOne(String name);
}
